/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import javax.persistence.criteria.Expression;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;

import com.taobao.snowsnow.hibernate.dao.TradeDao;
import com.taobao.snowsnow.hibernate.pojo.Trade;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class TradeDaoImpl extends BaseDaoImpl implements TradeDao {
	
	public Trade findById(int id) throws Exception {
		return (Trade) getHibernateTemplate().get(Trade.class, id);
	}
	
	public List<Trade> findByName(String name) throws Exception {
		return super.findByProperty(Trade.class, "name", name);
	}

	public List<Trade> findAll() throws Exception {
		return getHibernateTemplate().find("from Trade");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Trade.class);
	}

	public List<Trade> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Trade.class, page, limit);
	}

	@Override
	public Trade getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Trade as o where o.id = (select min(id) from Trade)");
		List<Trade> list = query.list();
		
		if(list.size() !=0)
			return (Trade) list.get(0);
		return null;
	}

	@Override
	public Trade getLastObject() throws Exception {
		Query query = getSession().createQuery("from Trade as o where o.id = (select max(id) from Trade)");
		List<Trade> list = query.list();
		if(list.size() !=0)
			return (Trade) list.get(0);
		else return null;
	}

	@Override
	public List<Trade> findByTaoBaoID(String nick) throws Exception {
		Query query = getSession().createQuery("from Trade as t where t.people.taobaoId = :nick");
		query.setString("nick", nick);
		return query.list();
	}

	@Override
	public Trade findByTid(Long tid) throws Exception {
		Query query = getSession().createQuery("from Trade as t where tid = :tid");
		query.setLong("tid", tid);
		List<Trade> list = query.list();
		if(list.size() !=0)
			return (Trade) list.get(0);
		return null;
	}

	@Override
	public Trade findByMultiTids(long tid) throws Exception {
		Query query = getSession().createQuery("from Trade as t where t.tradeTids like :tids");
		query.setString("tids", "%" +String.valueOf(tid) + "%");
		List<Trade> list = query.list();
		if(list.size() !=0)
			return (Trade) list.get(0);
		return null;
	}

	@Override
	public Trade findByTaoBaoIDAndStatus(String nick, int status) throws Exception {
		Query query = getSession().createQuery("from Trade as t where t.people.taobaoId = :nick and t.status = :status");
		query.setString("nick", nick);
		query.setInteger("status", status);
		List<Trade> list = query.list();
		if(list.size() !=0)
			return (Trade) list.get(0);
		return null;
	}

	@Override
	public List<Trade> findByDetachedCriteria() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Trade> findByCriteria(List<Criterion> expression)
			throws Exception {
		Criteria c = getSession().createCriteria(Trade.class);
		for(Criterion tmp : expression) {
			c.add(tmp);
		}
		return c.list();
	}
}
